package com.itheima.patterns.builderpattern.singleton.lazyman2;

public class Singleton {

    //私有构造方法
    private Singleton() {
    }

    //声明Singleton类型的变量instance，并未进行赋值
    private static volatile Singleton instance;

    public static Singleton getInstance() {
        //第一次检查，若instance不为null，则不进入抢锁阶段，直接返回实际值即可
        if (instance == null) {
            synchronized (Singleton.class) {
                //第二次检查，得到锁之后再次判断 instance 是否为空
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        Singleton instance1 = getInstance();
        Singleton instance2 = getInstance();
        System.out.println(instance1 == instance2);
    }
}